﻿using System;
using System.Linq;
using System.Reflection;
using FairyGUI;
using UnityEngine;
using Whoot.DI;

namespace Whoot.UI
{
    public abstract class UIPanel : IUIPanel
    {
        public virtual string UILayer => string.Empty;

        /// <summary>
        /// 是否显示时置顶
        /// </summary>
        public bool IsTop { get; set; }

        /// <summary>
        /// 是否显示黑色背景蒙版
        /// </summary>
        public bool IsModal { get; set; }

        public virtual bool IsShowing => ContentPane != null && ContentPane.parent != null;

        public virtual bool IsDotDel { get; set; }

        public virtual string UIPackName { get; set; }
        public virtual string UIResName { get; set; }

        /// <summary>
        /// 面板打开动画
        /// </summary>
        public Task ShowAnim = null;

        /// <summary>
        /// 面板关闭动画
        /// </summary>
        public Task HideAnim = null;

        private object _paramData;
        private bool _isInited;

        public GComponent ContentPane { get; set; }
        [Inject] protected IUIKit UIKit;

        public void SetData(object args)
        {
            _paramData = args;
        }

        public object GetData()
        {
            return _paramData;
        }

        public object GetPanelNode()
        {
            return ContentPane;
        }

        public void Init()
        {
            //实例化预设
            Debug.Log("实例化资源===");
            if (!_isInited)
            {
                UIPackage.AddPackage("fgui/" + this.UIPackName);

                GObject panelObj = UIPackage.CreateObject(this.UIPackName, this.UIResName);
                if (panelObj == null)
                {
                    throw new System.Exception("不存在包名：" + this.UIPackName + "/ResName=" + UIResName);
                }

                panelObj.SetSize(GRoot.inst.width, GRoot.inst.height);
                panelObj.position = Vector3.zero;
                panelObj.scale = Vector2.one;
                panelObj.pivotX = 0.5f;
                panelObj.pivotY = 0.5f;
                ContentPane = panelObj.asCom;
                this.AutoFindAllField();
                OnInit();
                _isInited = true;
            }
        }

        public void Show()
        {
            if (ShowAnim != null)
            {
                ShowAnim.OnFinished = OpenAnimFinished;
                ShowAnim.Execute();
            }
            else
            {
                this.OpenAnimFinished(true);
            }

            if (!IsShowing)
            {
                UIKit.LayerManager.AddChild(this);
            }
        }

        public void Hide()
        {
            if (!IsShowing) return;
            if (HideAnim != null)
            {
                HideAnim.OnFinished = HideAnimFinished;
                HideAnim.Execute();
            }
            else
            {
                this.HideAnimFinished(true);
            }
        }

        public void Refresh()
        {
            Show();
            if (IsTop)
            {
                UIKit.LayerManager.Top(this);
            }
        }

        public void HideImmediately()
        {
            if (ContentPane.parent != null)
            {
                UIKit.LayerManager.RemoveChild(this);
            }
        }

        public void Dispose()
        {
            if (!IsDotDel)
            {
                this.HideImmediately();
                this.ContentPane.Dispose();
                this.OnDestroy();
            }
            else
            {
                Debug.LogError("当前panel标记为不可删除，name=" + this.UIResName);
            }
        }

        /// <summary>
        /// 打开动画播放完成
        /// </summary>
        /// <param name="succeed"></param>
        private void OpenAnimFinished(bool succeed)
        {
            this.OnShow();
        }

        /// <summary>
        /// 关闭动画播放完成
        /// </summary>
        /// <param name="succeed"></param>
        private void HideAnimFinished(bool succeed)
        {
            this.OnHide();
            this.HideImmediately();
        }

        #region 接口

        /// <summary>
        /// 界面初始化的时候
        /// </summary>
        protected virtual void OnInit()
        {
        }

        /// <summary>
        /// 显示界面显示完成
        /// </summary>
        /// <param name="param"></param>
        protected virtual void OnShow()
        {
        }

        /// <summary>
        /// 界面隐藏的时候
        /// </summary>
        protected virtual void OnHide()
        {
        }

        /// <summary>
        /// 界面销毁的时候
        /// </summary>
        protected virtual void OnDestroy()
        {
        }

        #endregion
    }
}